package com.njupt.simulation.utils;

public class Antenna580 {
        // 计算增益G

    /**
     * 计算 D/λ 的方法
     * @param Gmax  最大增益
     * @param n  天线效率
     * @return  D/λ
     */
    private static double calculateDOverLambda(double Gmax, double n) {
        return Math.sqrt(Math.pow(10 ,Gmax / 10) / (n * Math.PI * Math.PI));
    }

    /**
     * 计算 G1 的方法
     * @param D_over_lambda   D/λ
     * @return  G1
     */
    private static double calculateG1(double D_over_lambda) {
        double G1 = 0;
        if (D_over_lambda < 50){
            G1 = 2 + 15 * Math.log10(D_over_lambda);
        }
        else if (D_over_lambda >= 50 && D_over_lambda < 100) {
            G1 = -21 + 25 * Math.log10(D_over_lambda);
        } else if (D_over_lambda >= 100){
            G1 = -1 + 15 * Math.log10(D_over_lambda);
        }
        return G1;
    }

    /**
     * 计算 phi_r 的方法
     * @param D_over_lambda    D/λ
     * @return  phi_r
     */
    private static double calculatePhiR(double D_over_lambda) {
        double phi_r = 0;
        if (D_over_lambda >= 100) {
            phi_r = 15.85 * Math.pow(D_over_lambda, -0.6);
        } else {
            phi_r = 100 / D_over_lambda;
        }
        return  phi_r;
    }

    /**
     * 计算phi_b的方法
     * @return  phi_b
     */
    private static double calculatePhiB() {
        return Math.pow(10, 42.0 / 25);
    }


    /**
     * 计算 G 的方法
     * @param phi  偏轴角
     * @param Gmax  主瓣最大增益
     * @param n  天线效率
     * @return  天线增益
     */
    public static double calculateG(double phi, double Gmax, double n) {
        double D_over_lambda =  calculateDOverLambda(Gmax, n);

        double G1 = calculateG1(D_over_lambda);
        double phi_m = 20 / D_over_lambda * Math.sqrt(Gmax - G1);
        double phi_r = calculatePhiR(D_over_lambda);
        double phi_b = calculatePhiB();
        double G = 0;

        if (D_over_lambda >= 50) {
            if (phi >= 0 && phi < phi_m) {
                G = Gmax - 2.5e-3 * Math.pow(D_over_lambda * phi, 2) ;
            } else if (phi >= phi_m && phi < phi_r) {
                G = G1;
            } else if (phi >= phi_r && phi <= 19.5) {
                G = 29 - 25 * Math.log10(phi);
            } else if (phi > 19.5 && phi < phi_b) {
                G = Math.min(-3.5, 32 - 25 * Math.log10(phi));
            } else if (phi >= phi_b && phi <= 180) {
                G = -10;
            }
        }
        if (D_over_lambda < 50) {

            if (phi >= 0 && phi < phi_m) {
                G = Gmax - 2.5e-3 * Math.pow(D_over_lambda, 2) * Math.pow(phi, 2);
            } else if (phi >= phi_m && phi < phi_r) {
                G = G1;
            } else if (phi >= phi_r && phi < phi_b) {
                G = 52 - 10 * Math.log10(D_over_lambda) - 25 * Math.log10(phi);
            } else if (phi >= phi_b && phi <= 180) {
                G = 10 - 10 * Math.log10(D_over_lambda);
            }
        }
        return  G;
    }

}
